package moxl.relaxng.model;

import moxl.DocumentCopier;
import moxl.dom.MoxlElement;
import moxl.dom.ethereal.EtherealElement;
import moxl.dom.mutable.MutableElement;
import moxl.relaxng.schema.RelaxNgSchemaElementType;
import moxl.relaxng.schema.RelaxNgSimplifier;
import moxl.relaxng.schema.SimplifiedRelaxNgGrammar;

public class EtherealRelaxNgSchema extends PatternParentSupport implements RelaxNgSchema
{
	public EtherealRelaxNgSchema()
	{
		super(new EtherealElement(RelaxNgSchemaElementType.grammar.getName()));
	}

	public boolean isValid(MoxlElement elementToValidate)
	{
		RelaxNgSimplifier relaxNgSimplifier = new RelaxNgSimplifier();
		MoxlElement src = getGrammarElement();
		DocumentCopier copier = new DocumentCopier();
		MutableElement dest = copier.copyElement(src);
		MoxlElement grammarElement = relaxNgSimplifier.simplify(dest);
		SimplifiedRelaxNgGrammar simplifiedRelaxNgGrammar = new SimplifiedRelaxNgGrammar(grammarElement);
		return simplifiedRelaxNgGrammar.matches(elementToValidate);
	}

	public MoxlElement getGrammarElement()
    {
	    return getElement();
    }

}
